/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.transfer.validation;

import com.transfer.annotation.LessThan;
import java.lang.annotation.Annotation;
import java.util.logging.Logger;
import org.apache.myfaces.extensions.validator.crossval.storage.CrossValidationStorageEntry;
import org.apache.myfaces.extensions.validator.crossval.strategy.AbstractCompareStrategy;

/**
 *
 * @author bkummel
 */
public class LessThanStrategy extends AbstractCompareStrategy<LessThan> {
    private static final Logger log = Logger.getLogger(LessThanStrategy.class.getName());

    @Override
    protected String getValidationErrorMsgKey(LessThan annotation, boolean isTargetComponent) {
        return annotation.validationErrorMsgKey();
    }

    @Override
    public boolean isViolation(Object object1, Object object2, LessThan annotation) {
        log.entering(this.getClass().getName(), "isViolation", new Object[]{object1, object2, annotation});
        if (object1 instanceof Number && object2 instanceof Number) {
            Number n1 = (Number)object1;
            Number n2 = (Number)object2;
            if (annotation.allowEqual()) {
                return !(n1.doubleValue() <= n2.doubleValue());
            } else {
                return !(n1.doubleValue() < n2.doubleValue());
            }
        } else {
            throw new IllegalArgumentException("Must be a numerical value");
        }
    }

    @Override
    public String[] getValidationTargets(LessThan annotation) {
        return annotation.value();
    }

    @Override
    protected boolean useTargetComponentToDisplayErrorMsg(CrossValidationStorageEntry crossValidationStorageEntry) {
        return false;
    }
}
